/**
 * @Date 2025/3/19 8:55
 * @Created by 衡盛永
 */

package com.shangting.web.annotation;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
 * 获取注解中的属性信息
 * <p>
 * 如果指定类型的注释存在于此元素上,  方法返回true
 * java.lang.Package.isAnnotationPresent(Class<? extends Annotation> annotationClass)
 * //如果是该类型的注释, 方法返回该元素的该类型的注释
 * java.lang.Package.getAnnotation(Class< A > annotationClass)
 */

public class ReflectAnnotationDemo {
    public static void main(String[] args) throws ClassNotFoundException {
        ReflectAnnotationDemo.getClassAnnotationInfo();
        ReflectAnnotationDemo.getFieldAnnotationInfo();
        ReflectAnnotationDemo.getMethodAnnotationInfo();
        ReflectAnnotationDemo.getParameterAnnotationInfo();
    }

    public static void getClassAnnotationInfo() throws ClassNotFoundException {
        Class c1 = Class.forName("com.shangting.web.annotation.User");

        if(User.class.isAnnotationPresent(AnnotationClass.class)){
            System.out.println(User.class.getAnnotation(AnnotationClass.class).value());
        }
        // 获取类变量注解
        Field[] fields = User.class.getDeclaredFields();
        for (Field f : fields) {
            if(f.isAnnotationPresent(AnnotationProperty.class)){
                System.out.print(f.getAnnotation(AnnotationProperty.class).value() + ",");
            }
        }
    }

    public static void getParameterAnnotationInfo() throws ClassNotFoundException {
        Class c2 = Class.forName("com.shangting.web.annotation.User");
        // 获取方法参数注解
        Method[] methods2 = User.class.getDeclaredMethods();
        for (Method m : methods2) {
            // 获取方法的所有参数
            Parameter[] parameters = m.getParameters();
            for (Parameter p : parameters) {
                // 判断是否存在注解
                if (p.isAnnotationPresent(AnnotationParameter.class)) {
                    System.out.println(p.getAnnotation(AnnotationParameter.class).value());
                }
            }
        }
    }

    public static void getMethodAnnotationInfo() throws ClassNotFoundException {
        Class c2 = Class.forName("com.shangting.web.annotation.User");
        // 获取方法注解：
        Method[] methods = User.class.getDeclaredMethods();
        for (Method m : methods) {
            if (m.isAnnotationPresent((Class<? extends Annotation>) AnnotationMethod.class)) {
                System.out.println(m.getAnnotation(AnnotationMethod.class).value());
            }
        }
    }

    public static void getFieldAnnotationInfo() throws ClassNotFoundException {
        Class c1 = Class.forName("com.shangting.web.annotation.User");

        if (User.class.isAnnotationPresent(AnnotationProperty.class)) {
            System.out.println(User.class.getAnnotation(AnnotationProperty.class).value());
        }
        // 获取类变量注解
        Field[] fields = User.class.getDeclaredFields();
        for (Field f : fields) {
            if (f.isAnnotationPresent(AnnotationProperty.class)) {
                System.out.print(f.getAnnotation(AnnotationProperty.class).value() + ",");
            }
        }
    }
}
